package chenji.gunapi.listeners;

import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Damageable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.inventory.ItemStack;

import chenji.gunapi.GunAPI;
import chenji.gunapi.bullet.Bullet;
import chenji.gunapi.events.GunHitEntityEvent;
import chenji.gunapi.events.MouseClickEvent;
import chenji.gunapi.gun.Gun;
import chenji.gunapi.untl.Tellraw;

@SuppressWarnings("deprecation")
public class Listeners implements Listener {

	@EventHandler
	public void TestEvent(PlayerChatEvent e) {
		if (e.getMessage().equalsIgnoreCase("getGUN")) {
			e.getPlayer().getInventory().addItem(GunAPI.getGunAPI().getGunManager().getFinalItem(GunAPI.getGunAPI().getGunManager().getGun("M4A1")));
		}
		if (e.getMessage().equalsIgnoreCase("getGUN2")) {
			e.getPlayer().getInventory().addItem(GunAPI.getGunAPI().getGunManager().getFinalItem(GunAPI.getGunAPI().getGunManager().getGun("手枪")));
		}
		if (e.getMessage().equalsIgnoreCase("test1")) {
			Tellraw.send(e.getPlayer(), null, "");
		}
	}

	@EventHandler
	public void TestEvent(PlayerItemHeldEvent e) {
		if (e.isCancelled()) {
			return;
		}
		ItemStack i = e.getPlayer().getInventory().getItem(e.getNewSlot());
		if (i == null) {
			if (GunAPI.getGunAPI().getPlayerGunManager().isHolding(e.getPlayer())) {
				GunAPI.getGunAPI().getPlayerGunManager().putDownGun(e.getPlayer());
			
				e.getPlayer().sendMessage("§c你放下了你手中的枪");
			}
			return;
		}
		Gun g = GunAPI.getGunAPI().getGunManager().getGun(i);
		if (g == null) {
			if (GunAPI.getGunAPI().getPlayerGunManager().isHolding(e.getPlayer())) {

				GunAPI.getGunAPI().getPlayerGunManager().putDownGun(e.getPlayer());
			
				e.getPlayer().sendMessage("§c你放下了你手中的枪");
			}

			return;
		}
		GunAPI.getGunAPI().getPlayerGunManager().holdGun(g, e.getPlayer());
		e.getPlayer().sendMessage("§c你拿起了" + g.getName());

	}

	@EventHandler
	public void mouse(MouseClickEvent e) {
		System.out.println(e.getState());
		GunAPI.getGunAPI().getPlayerGunManager().updateMouseState(e.getType(), e.getState(), e.getPlayer());
	}

	@EventHandler
	public void event2(ProjectileHitEvent e) {
		if (e.getEntity() == null) {
			return;
		}
		if (!(e.getEntity() instanceof Arrow)) {
			return;
		}
		if (!(GunAPI.getGunAPI().getBulletMnagaer().contain(e.getEntity()))) {
			return;
		}

		GunAPI.getGunAPI().getBulletMnagaer().createWaitRemove(e.getEntity());
	}

	@EventHandler
	public void event(EntityDamageByEntityEvent e) {
		if (e.getDamager() == null) {
			return;
		}
		if (e.isCancelled()) {
			return;
		}
		if (!(e.getDamager() instanceof Arrow)) {
			return;
		}
		Bullet b = GunAPI.getGunAPI().getBulletMnagaer().getBullet(e.getDamager());

		if (b == null) {
			return;
		}
		b.hitEntity();
		GunHitEntityEvent eve = new GunHitEntityEvent(b.getGun(), b.getDamager(), e.getDamage(), e.getEntity());
		Bukkit.getPluginManager().callEvent(eve);
		if (eve.isCancelled()) {
			e.setCancelled(true);
			return;
		}
		e.setDamage(b.getDamage());
		if (!(eve.getGun().isKnockBack())) {
			Damageable a = null;

			if (e.getEntity() instanceof Damageable) {
				a = (Damageable) e.getEntity();
			}
			if (a == null) {
				return;
			}

			double i = e.getFinalDamage();
			e.setCancelled(true);
			double health = a.getHealth() - i;
			Location l = (GunAPI.getGunAPI().getUntl().getHeadLocation(e.getEntity()));
			l.getWorld().playEffect(l, Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
			if (health < 0) {
				a.setHealth(0);
				return;
			}
			a.setHealth(a.getHealth() - i);

		}
		GunAPI.getGunAPI().getBulletMnagaer().removeBullet(e.getDamager());
	}

	@EventHandler
	public void onplayerjoin(PlayerJoinEvent e) {
		GunAPI.getGunAPI().getSuperManager().onPlayerJoin(e.getPlayer());
	}

	@EventHandler
	public void onplayerjoin2(PlayerQuitEvent e) {
		GunAPI.getGunAPI().getSuperManager().onPlayerQuit(e.getPlayer());
	}

	@EventHandler
	public void onPlayerSit(PlayerToggleSneakEvent e) {
		GunAPI.getGunAPI().getPlayerState().onPlayerSit(e.getPlayer());

	}

}
